package com.sgb.capital.view.pop;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.widget.TextView;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.contrarywind.view.WheelView;
import com.sgb.capital.R;
import com.sgb.capital.callback.Constants;
import com.sgb.capital.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import razerdp.basepopup.BasePopupWindow;
import razerdp.util.animation.AnimationHelper;
import razerdp.util.animation.Direction;
import razerdp.util.animation.TranslationConfig;

/**
 * 作者:张磊
 * 日期:2021/4/16 0016
 * 说明:筛选弹框
 */
public class SelPop extends BasePopupWindow {


    public String mTip;
    public List<String> mList;
    private WheelView mWheelView;
    public TextView mTvTitleTip;
    private View mErrorView;

    public SelPop(Context context) {
        super(context);
    }

    @Override
    public View onCreateContentView() {
        View v = createPopupById(R.layout.cut_pop);
        mWheelView = v.findViewById(R.id.wv);
        v.findViewById(R.id.tv_cancel).setOnClickListener(v1 -> dismiss());
        mErrorView = v.findViewById(R.id.ll_null);
        mList = new ArrayList<>();
        mTvTitleTip = v.findViewById(R.id.tv_title_tip);
        mWheelView.setOnItemSelectedListener(index -> {
            mTip = mList.get(index);
        });
        TextView tv_sure = v.findViewById(R.id.tv_sure);
        tv_sure.setOnClickListener(v1 -> {
            dismiss();
            Utils.sendMsg(Constants.EVENT_SELECT, mTip);
        });
        return v;
    }

    @Override
    protected Animation onCreateShowAnimation() {
        return AnimationHelper.asAnimation()
                .withTranslation(new TranslationConfig()
                        .from(Direction.BOTTOM))
                .toShow();
    }

    @Override
    protected Animation onCreateDismissAnimation() {
        return AnimationHelper.asAnimation()
                .withTranslation(new TranslationConfig()
                        .to(Direction.BOTTOM))
                .toDismiss();
    }


    public void show(int index, View view) {
        if (mList.size() == 0 || mList.get(0).contains("无")) {
            mErrorView.setVisibility(View.VISIBLE);
            mWheelView.setVisibility(View.GONE);
            setPopupGravity(Gravity.TOP);
            showPopupWindow(view);
            return;
        }
        mWheelView.setCurrentItem(index);
        mWheelView.setCyclic(false);
        mWheelView.setAdapter(new ArrayWheelAdapter(mList));
        mWheelView.setCurrentItem(index);
        setPopupGravity(Gravity.TOP);
        showPopupWindow(view);
    }
}